package qc.module.duty.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;
import qc.common.core.constants.QCAuthConstant;
import qc.common.core.exception.QCPromptException;
import qc.module.duty.dto.team.DutyTeamAddMembersDto;
import qc.module.duty.dto.team.DutyTeamDto;
import qc.module.duty.dto.team.DutyTeamMemberDto;
import qc.module.duty.service.DutyTeamService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @projectName: qcdp
 * @package: qc.module.duty.api.controller
 * @className: TeamController
 * @date: 2023/8/9
 * @version: 1.0
 */
@RestController
@RequestMapping("/team")
public class TeamController {

    private DutyTeamService dutyTeamService;

    @Autowired
    public void setRestTemplate(DutyTeamService dutyTeamService) {
        this.dutyTeamService = dutyTeamService;
    }

    @Autowired
    HttpServletRequest request;

    @Autowired
    private RestTemplatePlatform restTemplatePlatform;

    /**
     * 获取所有值班班组信息，按值班分组排序号升序
     *
     * @return List<DutyTeamDto> 返回结果集
     * @author QcCheng Tech
     * @date 2023/8/9
     */
    @RequestMapping(value = "/all", method = {RequestMethod.GET, RequestMethod.POST})
    public List<DutyTeamDto> getAll() {
        return dutyTeamService.query(null, true);
    }

    /**
     * 查询当前用户有效部门值班班组信息，按值班分组排序号升序
     *
     * @return List<DutyTeamDto> 返回结果集
     * @author QcCheng Tech
     * @date 2023/8/9
     */
    @RequestMapping(value = "/dept", method = {RequestMethod.GET, RequestMethod.POST})
    public List<DutyTeamDto> getByUserDept() throws QCPromptException {
        //判断查询条件，如果需要根据部门过滤，使用微服务调用根据用户ID获取部门集合信息
        HttpHeaders headers = new HttpHeaders();
        headers.add(QCAuthConstant.TOKEN_HEADER_KEY, request.getHeader(QCAuthConstant.TOKEN_HEADER_KEY));
        headers.add(QCAuthConstant.PROJECT_CODE_HEADER_KEY, request.getHeader(QCAuthConstant.PROJECT_CODE_HEADER_KEY));
        List<Integer> userRelatedDeptIds = restTemplatePlatform.getUserRelatedDeptIds(headers);

        return dutyTeamService.query(userRelatedDeptIds, false);
    }

    /**
     * 获取指定的值班班组信息
     * @param id: 指定的值班班组id
     * @return DutyTeamDto 返回结果集
     * @author QcCheng Tech
     * @date 2023/8/9
     */
    @RequestMapping(value = "/get", method = {RequestMethod.GET})
    public DutyTeamDto getByParam(@RequestParam int id) {
        return dutyTeamService.get(id);
    }

    /**
     * 获取指定的值班班组信息
     * @param id: 指定的值班班组id
     * @return DutyTeamDto 返回结果集
     * @author QcCheng Tech
     * @date 2023/8/9
     */
    @RequestMapping(value = "/get/{id}", method = {RequestMethod.GET})
    public DutyTeamDto getByPath(@PathVariable(required = true) int id) {
        return dutyTeamService.get(id);
    }

    /**
     * 添加值班组
     *
     * @param dto:对象
     * @return String 添加成功返回null,错误返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/9
     */
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public String add(@RequestBody DutyTeamDto dto) {
        return dutyTeamService.add(dto);
    }

    /**
     * 修改值班组
     *
     * @param dto 值班组信息
     * @return 成功返回null
     * @author QuCheng Tech
     * @date 2023/8/9
     */
    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.POST})
    public String update(@RequestBody DutyTeamDto dto) {
        return dutyTeamService.update(dto);
    }

    /**
     * 删除值班组
     *
     * @param id 值班组ID
     * @return 成功返回null
     * @author QcCheng Tech
     * @since 2023/5/25
     */
    @RequestMapping(value = "/delete", method = {RequestMethod.DELETE})
    public String deleteByParam(@RequestParam int id) {
        return dutyTeamService.delete(id);
    }

    /**
     * 删除
     *
     * @param id 栏目ID
     * @return 成功返回null
     * @author QcCheng Tech
     * @since 2023/5/25
     */
    @RequestMapping(value = "/delete/{id}", method = {RequestMethod.DELETE})
    public String deleteByPath(@PathVariable(required = true) int id) {
        return dutyTeamService.delete(id);
    }

    /**
     * 获取指定班值的成员列表
     *
     * @param id: 值班表id
     * @return DutyTeamMemberDto 返回信息
     * @author QcCheng Tech
     * @date 2023/8/10
     */
    @RequestMapping(value = "/members", method = {RequestMethod.GET})
    public List<DutyTeamMemberDto> getTeamMembersByParam(@RequestParam int id) {
        return dutyTeamService.getTeamMembers(id);
    }

    /**
     * 获取指定班值的成员列表
     *
     * @param id: 值班表id
     * @return DutyTeamMemberDto 返回信息
     * @author QcCheng Tech
     * @date 2023/8/10
     */
    @RequestMapping(value = "/members/{id}", method = {RequestMethod.GET})
    public List<DutyTeamMemberDto> getTeamMembersByPath(@PathVariable(required = true) int id) {
        return dutyTeamService.getTeamMembers(id);
    }

    /**
     * 添加班值成员
     *
     * @param dto 班值添加成员DTO
     * @return 成功返回null
     * @author QcCheng Tech
     * @date 2023/8/12
     */
    @RequestMapping(value = "/members", method = {RequestMethod.POST})
    public String addTeamMembers(@RequestBody DutyTeamAddMembersDto dto) {
        return dutyTeamService.addTeamMembers(dto);
    }

    /**
     * 设置班值成员信息，后台判断存在时进行更新，不存在时进行新增；
     *
     * @param dutyTeamDto:设置信息
     * @return String 成功返回null
     * @author QcCheng Tech
     * @date 2023/8/10
     */
    @RequestMapping(value = "/member", method = {RequestMethod.POST})
    public String setTeamMember(@RequestBody DutyTeamMemberDto dutyTeamDto) {
        return dutyTeamService.setTeamMember(dutyTeamDto);
    }

    /**
     * 删除指定的班值成员
     *
     * @param dutyTeamDto:传入班值ID和成员ID；
     * @return String 成功返回null 失败返回错误信息
     * @author QcCheng Tech
     * @date 2023/8/11
     */
    @RequestMapping(value = "/member", method = {RequestMethod.DELETE})
    public String deleteTeamMember(@RequestBody DutyTeamMemberDto dutyTeamDto)  {
        return dutyTeamService.deleteTeamMember(dutyTeamDto);
    }
}
